
/*
 * START            : Initial state. Point start. the appropriate serve area 
 *                    is highlighted. The right left serv is decided here based
 *                    on the point.
 * SERV_READY       : Serv position ready. Enables the serv err boxes and 
 *                    the right service court in the top court.
 * INPLAY           : Ball is inplay. Serv is successful. Enable all serv err areas.
 * FIRST_SERV       : First serve error. The err areas are more pronounced.
 * END              : Point is over. Score it and find a way to go back to START.
 *
 */

var STATES = {INIT:0, START_SERV:1, START_RECV:2, SERV_READY:3, RECV_READY:4, INPLAY:5, END:6};
var curState = STATES.INIT;
var p1=new Player("P1", 1);
var p2=new Player("P2", 2);
var toggleServ=0;
var toggleRecv=0;
var first_serv=0;

function init() {
    p1.updateScore();
    p2.updateScore();
    $('#lsc_ace, #lsc_winner, #lsc_return').hide();
    $('#rsc_ace, #rsc_winner, #rsc_return').hide();
    $('#b_lsc_ace, #b_lsc_winner, #b_lsc_return').hide();
    $('#b_rsc_ace, #b_rsc_winner, #b_rsc_return').hide();

}
    
function Player(name, id) {
    this.id = id;
    this.name = name;
    this.score = '00';
    this.game_over = 0;
    this.set1 = 0;
    this.set2 = 0;
    this.set3 = 0;

    this.dbl_flt=0;
    this.net_err=0;
    this.serv_try=0;
    this.serves=0;
    this.aces=0;

    this.incScore = function() {
        this.game_over = 0;
        switch (this.score) {
        case '00':
            this.score=15;
            break;
        case 15:
            this.score=30;
            break;
        case 30:
            this.score=40;
            break;
        case 40:
            if(this.id == 1) {
                p = p2;
            }
            else if(this.id == 2) {
                p = p1;
            }

            if(p.score == 40) {
                this.score = 'A';
            }
            else if(p.score == 'A') {
                this.score == 40;
                p.score = 40;
            }
            else {
                this.game_over = 1;
                this.set1++;
                p1.score='00';
                p2.score='00';
            }
            break;
        case 'A':
            this.game_over = 1;
            this.set1++;
            p1.score='00';
            p2.score='00';
            break;
        }

        this.updateScore();
    }
    
    //XXX should be in scoreboard object
    this.updateScore = function() {
        $('#p1_score').html('' + p1.score);
        $('#p1_name').html('' + p1.name);
        $('#p1_set1').html('' + p1.set1);
        $('#p1_set2').html('' + p1.set2);
        $('#p1_set3').html('' + p1.set3);
        $('#p2_score').html('' + p2.score);
        $('#p2_name').html('' + p2.name);
        $('#p2_set1').html('' + p2.set1);
        $('#p2_set2').html('' + p2.set2);
        $('#p2_set3').html('' + p2.set3);
    }
}

function startServ() {
    if(this.id == "servposr") {
        toggleServ = 0;
    }
    else {
        toggleServ = 1;
    }
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlightr');
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlight');
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').unbind('click', execute);
            
    $('#lsc_ace, #lsc_winner, #lsc_return').hide();
    $('#rsc_ace, #rsc_winner, #rsc_return').hide();

    curState = STATES.START_SERV;
    execute();
}

function startRecv() {
    if(this.id == "recvposr") {
        toggleRecv = 0;
    }
    else {
        toggleRecv = 1;
    }
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlightr');
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlight');
    $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').unbind('click', execute);

    $('#b_lsc_ace, #b_lsc_winner, #b_lsc_return').hide();
    $('#b_rsc_ace, #b_rsc_winner, #b_rsc_return').hide();

    curState = STATES.START_RECV;
    execute();
}

function execute() {
    switch (curState) {
    case STATES.INIT:
        break;
    case STATES.START_SERV:
        //bind for SERV_READY
        if(toggleServ == 0) {
            $('#rse, #cm, #lse, #lsc').addClass('highlightr');
            $('#rsc_ace, #rsc_winner, #rsc_return').show();
            $('#rsc_ace, #rsc_winner, #rsc_return').bind('click', execute);
        }
        else if(toggleServ == 1) {
            $('#rse, #cm, #lse, #rsc').addClass('highlightr');
            $('#lsc_ace, #lsc_winner, #lsc_return').show();
            $('#lsc_ace, #lsc_winner, #lsc_return').bind('click', execute);
        }
        $('#rse, #cm, #lse, #lsc, #rsc').bind('click', execute);
        $('#comment').html('State = ' + curState + '->SERV_READY');
        curState = STATES.SERV_READY;
        break;
    case STATES.SERV_READY:
        //use a variable for the id, to reduce multiple checks
        if(toggleServ == 0) {
            id1="rsc";
            id2="lsc";
        }
        else if(toggleServ == 1) {
            id1="lsc";
            id2="rsc";
        }

        //if serving from right side this click is an INPLAY
        if(this.id == id1) {
            //unbind
            $('#rse, #cm, #lse, #lsc, #rsc').removeClass('highlightr');
            $('#rsc').removeClass('highlight');
            $('#lsc').removeClass('highlight');
            $('#rse, #cm, #lse, #lsc, #rsc').unbind('click', execute);

            first_serv = 0;

            //bind for INPLAY
            $('#rse, #b_rse, #lse, #b_lse').addClass('highlightr');
            $('#rse, #b_rse, #lse, #b_lse').bind('click', execute);
            $('#comment').html('State = ' + curState + '->INPLAY');
            curState = STATES.INPLAY;
        }
        //if serving from right side this click is a FIRST_SERV
        else {
            if(first_serv == 1) {
                $('#comment').html('State = ' + curState + '->END');
                first_serv = 0;
                p2.incScore();
                curState = STATES.END;
                execute();
                break;
            }
            first_serv = 1;
            //since its a first serve, keep all highlightling and maintain the state.
            $('#'+id1+'').addClass('highlightr');
            $('#comment').html('State = ' + curState + '->SERV_READY');
            curState = STATES.SERV_READY;
        }
        break;
    case STATES.START_RECV:
        //bind for RECV_READY
        if(toggleRecv == 0) {
            $('#b_rse, #b_cm, #b_lse, #b_lsc').addClass('highlightr');
            $('#b_rsc').addClass('highlight');
            $('#b_rsc_ace, #b_rsc_winner, #b_rsc_return').show();
            $('#b_rsc_ace, #b_rsc_winner, #b_rsc_return').bind('click', execute);
        }
        else if(toggleRecv == 1) {
            $('#b_rse, #b_cm, #b_lse, #b_rsc').addClass('highlightr');
            $('#b_lsc').addClass('highlight');
            $('#b_lsc_ace, #b_lsc_winner, #b_lsc_return').show();
            $('#b_lsc_ace, #b_lsc_winner, #b_lsc_return').bind('click', execute);
        }
        $('#b_rse, #b_cm, #b_lse, #b_lsc, #b_rsc').bind('click', execute);
        $('#comment').html('State = ' + curState + '->RECV_READY');
        curState = STATES.RECV_READY;
        break;
    case STATES.RECV_READY:
        //use a variable for the id, to reduce multiple checks
        if(toggleRecv == 0) {
            id1="b_rsc";
            id2="b_lsc";
        }
        else if(toggleRecv == 1) {
            id1="b_lsc";
            id2="b_rsc";
        }

        //if serving from right side this click is an INPLAY
        if(this.id == id1) {
            //unbind
            $('#b_rse, #b_cm, #b_lse, #b_lsc, #b_rsc').removeClass('highlightr');
            $('#b_rse, #b_cm, #b_lse, #b_lsc, #b_rsc').unbind('click', execute);
            $('#b_rsc').removeClass('highlight');
            $('#b_lsc').removeClass('highlight');

            first_serv = 0;

            //bind for INPLAY
            $('#rse, #b_rse, #lse, #b_lse').addClass('highlightr');
            $('#rse, #b_rse, #lse, #b_lse').bind('click', execute);
            $('#comment').html('State = ' + curState + '->INPLAY');
            alert("inplay");
            curState = STATES.INPLAY;
        }
        //if serving from right side this click is a FIRST_SERV
        else {
            if(first_serv == 1) {
                $('#comment').html('State = ' + curState + '->END');
                first_serv = 0;
                p1.incScore();
                curState = STATES.END;
                execute();
                break;
            }
            first_serv = 1;
            //since its a first serve, keep all highlightling and maintain the state.
            $('#'+id1+'').addClass('highlightr');
            $('#comment').html('State = ' + curState + '->RECV_READY');
            curState = STATES.RECV_READY;
        }
        break;
    case STATES.INPLAY:
        //score
        if((this.id == "rse") || (this.id == "lse")) {
            p2.incScore();
            $('#comment').html('INPLAY -> P2_WIN');
        }
        else {
            p1.incScore();
            $('#comment').html('INPLAY -> P1_WIN');
        }
        curState = STATES.END;
        execute();
        break;
    case STATES.END:
        //unbind
        $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlightr');
        $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').removeClass('highlight');
        $('#rse, #b_rse, #lse, #b_lse, #lsc, #rsc, #b_lsc, #b_rsc, #cm, #b_cm').unbind('click', execute);

        $('#comment').html('Tap Serv to start a new point');
        break;
    default:
        break;
    }
}
